package com.spzx.cart.listener;

import com.spzx.common.rabbitmq.SpzxRabbitmqService;
import com.spzx.common.rabbitmq.constants.SpzxRabbitmqConstants;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

@Component
public class SpzxCartMqConfig {
    //创建死信队列 死信交换机 绑定
    @Bean
    public Exchange cartDeadExchange(){
        return ExchangeBuilder.topicExchange(SpzxRabbitmqConstants.CART_DEAD_EXCHANGE)
                .durable(true)
                .build();
    }
    @Bean
    public Queue cartDeadQueue(){
        return QueueBuilder.durable(SpzxRabbitmqConstants.CART_DEAD_QUEUE)
                .build();
    }
    @Bean
    public Binding cartDeadBinding( Exchange cartDeadExchange ,Queue cartDeadQueue ){
        return BindingBuilder
                .bind(cartDeadQueue)
                .to(cartDeadExchange)
                .with(SpzxRabbitmqConstants.CART_DEAD_ROUTING_KEY)
                .noargs();
    }

    //创建业务队列 绑定死信交换机 业务交换机 绑定
    @Bean
    public Exchange orderSubmitExchange(){ //交换机队列 可以重复多次创建  但是配置必须一样
        return ExchangeBuilder.topicExchange(SpzxRabbitmqConstants.ORDER_SUBMIT_EXCHANGE)
                .durable(true)
                .build();
    }
    @Bean
    public Queue cartOrderSubmitQueue(){
        return QueueBuilder.durable(SpzxRabbitmqConstants.ORDER_SUBMIT_CART_QUEUE)
                //给业务队列绑定 死信交换机 和路由key
                //当业务队列的消息变为死信时，消息会被丢弃 由队列配置的死信交换机按照死信路由key分发
                .deadLetterExchange(SpzxRabbitmqConstants.CART_DEAD_EXCHANGE)
                .deadLetterRoutingKey(SpzxRabbitmqConstants.CART_DEAD_ROUTING_KEY)
                .build();
    }
    @Bean
    public Binding cartOrderSubmitBinding( Exchange orderSubmitExchange ,Queue cartOrderSubmitQueue ){
        return BindingBuilder
                .bind(cartOrderSubmitQueue)
                .to(orderSubmitExchange)
                .with(SpzxRabbitmqConstants.ORDER_SUBMIT_ROUTING_KEY)
                .noargs();
    }
}
